package org.zl.mybatis.use;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import org.zl.mybatis.use.mapper.CompanyMapper;
import org.zl.mybatis.use.meiju.AppUser;
import org.zl.mybatis.use.meiju.AppUserMapper;
import org.zl.mybatis.use.meiju.AppUserStatus;
import org.zl.mybatis.use.model.Company;

/**
 * 测试
 * @author ben
 * @date 2021-11-22 21:47:20 CST
 */
@Component
public class TestRunner implements CommandLineRunner {

	@Autowired
	private CompanyMapper cm;
	
	@Autowired
	private AppUserMapper aum;
	
	@Override
	public void run(String... args) throws Exception {
		System.out.println("cm=" + cm);
		
//		Company c = new Company();
//		c.setNameCn("公司112303");
//		c.setFounder("老板112303");
//
//		// 失败
//		int ret = cm.insert(c);
//		System.out.println("1 ret=" + ret + ", c=" + c);
//		
//		// 使用 targetRuntime="MyBatis3Simple" 时，没有 insertSelective方法了
//		Company c2 = new Company();
//		c2.setNameCn("公司112302");
//		c2.setFounder("老板112302");
//		c2.setClockInTimes(100_100_100L);
//		c2.setMarketValue(10_000_000_000.00);
//		ret = cm.insertSelective(c2);
//		System.out.println("2 ret=" + ret + ", c2=" + c2);
		
		// 一对一
//		Company c = cm.selectCompanyWithCeoById(2);
//		System.out.println("1.c=" + c);
//
//		c = cm.selectCompanyWithCeoById2(2);
//		System.out.println("2.c=" + c);
//
//		c = cm.selectCompanyWithCeoById3(2);
//		System.out.println("3.c=" + c);
//
//		c = cm.selectCompanyWithCeoById4(2);
//		System.out.println("4.c=" + c);
//
//		c = cm.selectCompanyWithCeoById5(2);
//		System.out.println("5.c=" + c);
//
//		c = cm.selectCompanyWithCeoById6(2);
//		System.out.println("6.c=" + c);

		// 一对多
//		c = cm.selectCompanyWithCeoById7(2);
//		System.out.println("一对多：7.c=" + c);
//
//		c = cm.selectCompanyWithCeoById8(2);
//		System.out.println("一对多：8.c=" + c);
//
//		c = cm.selectCompanyWithCeoById9(2);
//		System.out.println("一对多：9.c=" + c);
		
//		appUserTest();
		
		testInterceptor();
	}
	private void appUserTest() {
		System.out.println("0.aum=" + aum);
		
		AppUser au = new AppUser();
		au.setName("au-" + System.currentTimeMillis());
		au.setStatus(AppUserStatus.GOOD);
		
		System.out.println("1.au=" + au);
		Integer ret = aum.insert(au);
		System.out.println("ret=" + ret + ", 2.au=" + au);
		
		
	}
	
	/**
	 * 测试拦截器
	 * @author ben
	 * @date 2021-11-30 21:03:29 CST
	 */
	private void testInterceptor() {
		Company c = cm.selectCompanyWithCeoById(2);
		System.out.println("1.c=" + c);
		
		c.setNameCn("1130公司");
		cm.updateByPrimaryKey(c);
		System.out.println("after update.c=" + c);
		
		c.setId(null);
		c.setNameCn("1130公司02");
		cm.insert(c);
		
	}


}
